<?php

namespace app\model;

//use support\Model;
use app\controller\wmapi\IndexController;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaOrdersQuotation
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_orders_quotation';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all()
    {
        $condition [] = ['is_show','=',1];
        $list = Db::table('wa_orders_quotation')
            ->select('*')
            ->get();

        return json_encode($list);
    }


    public static function created($data){
        $result = Db::table('wa_orders_quotation')->insertGetId($data);
        return $result;
    }

    public static function updated($data,$where){
        $result = Db::table('wa_orders_quotation')->where($where)->update($data);
        return $result;
    }

    public static function deleted($where){
        $result = Db::table('wa_orders_quotation')->where($where)->delete();
        return $result;
    }

    public static function list($page, $pageNum, $whereNotIn,$fields, $up_between, $sort,$whereIn, $condition = [], $between = []){
        $offset = ($page - 1) * $pageNum;
        if ($whereIn){
            if ($between) {
                if ($up_between){
                    $model = Db::table('wa_orders_quotation')
                        ->select($fields)
                        ->where($condition)
                        ->whereIn('admin_id',$whereIn)
                        ->whereBetween('updated_at', $between)
                        ->whereNotIn('status',$whereNotIn);
                }else{
                    $model = Db::table('wa_orders_quotation')
                        ->select($fields)
                        ->where($condition)
                        ->whereIn('admin_id',$whereIn)
                        ->whereBetween('created_at', $between)
                        ->whereNotIn('status',$whereNotIn);
                }
            } else {
                if ($up_between){
                    $model = Db::table('wa_orders_quotation')
                        ->select($fields)
                        ->where($condition)
                        ->whereIn('admin_id',$whereIn)
                        ->whereBetween('updated_at', $between)
                        ->whereNotIn('status',$whereNotIn);
                }else{
                    $model = Db::table('wa_orders_quotation')
                        ->select($fields)
                        ->where($condition)
                        ->whereIn('admin_id',$whereIn)
                        ->whereNotIn('status',$whereNotIn);
                }

            }
        }else{
            if ($between) {
                if ($up_between){
                    $model = Db::table('wa_orders_quotation')
                        ->select($fields)
                        ->where($condition)
                        ->whereBetween('updated_at', $between)
                        ->whereNotIn('status',$whereNotIn);
                }else{
                    $model = Db::table('wa_orders_quotation')
                        ->select($fields)
                        ->where($condition)
                        ->whereBetween('created_at', $between)
                        ->whereNotIn('status',$whereNotIn);
                }
            } else {
                if ($up_between){
                    $model = Db::table('wa_orders_quotation')
                        ->select($fields)
                        ->where($condition)
                        ->whereBetween('updated_at', $between)
                        ->whereNotIn('status',$whereNotIn);
                }else{
                    $model = Db::table('wa_orders_quotation')
                        ->select($fields)
                        ->where($condition)
                        ->whereNotIn('status',$whereNotIn);
                }

            }
        }

        $count = $model->count();
        if($sort == 2){
            $list = $model
//                ->groupBy('quotation_order_sn')
                ->orderBy('updated_at', 'desc')
                ->offset($offset)
                ->limit($pageNum)
                ->get();
        }else{

            $list = $model
//                ->groupBy('quotation_order_sn')
                ->orderBy('created_at', 'desc')
                ->offset($offset)
                ->limit($pageNum)
                ->get();
//            ->toSql();
        }
//        ->paginate($pageNum);
        if ($list){

            foreach ($list as $k){

                if ($k->including_model){
                    $k->including_model = json_decode($k->including_model);
                }

                $sum = WaOrdersQuotationRelevance::sums(['order_sn_parent' => $k->quotation_order_sn]);
                $counts = WaOrdersQuotationRelevance::counts(['order_sn_parent' => $k->quotation_order_sn]);
                $k->totalPrice = $sum;
                $k->counts = $counts;

                // 报价单状态 0 草稿 1 待客户确认 2 待下单 3 已完成 4已作废 5 删除
                if($k->status == 0){
                    $k->status_name = '草稿';
                }elseif ($k->status == 1){
                    $k->status_name = '待客户确认';
                }elseif ($k->status == 2){
                    $k->status_name = '待下单';
                }elseif ($k->status == 3){
                    $k->status_name = '已完成';
                }elseif ($k->status == 4){
                    $k->status_name = '已作废';
                }elseif ($k->status == 5){
                    $k->status_name = '删除';
                }else{
                    $k->status_name = '';
                }
                // 订单类型 1 预售待采购   2  库存待销售 3 部分库存待销售
                if ($k->orders_type == 1){
                    $k->orders_type_name = '库存待销售';
                }elseif ($k->orders_type == 2){
                    $k->orders_type_name = '部分库存待销售';
                }elseif ($k->orders_type == 3){
                    $k->orders_type_name = '已完成';
                }else{
                    $k->orders_type_name = '';
                }

                // 交货条件 1 FAS  2 FOB  3 CIF  4 CIP
//                if ($k->condition == 1){
//                    $k->condition_name = 'FCA';
//                }elseif ($k->condition == 2){
//                    $k->condition_name = 'FOB';
//                }elseif ($k->condition == 3){
//                    $k->condition_name = 'CIF';
//                }elseif ($k->condition == 4){
//                    $k->condition_name = 'CIP';
//                }else{
//                    $k->condition_name = '';
//                }
                $k->condition_name = IndexController::conditionName($k->condition);

                // 货币类型   1 人民币 2 美元 3 欧元
//                if ($k->currency == 1){
//                    $k->currency_name = '人民币';
//                }elseif ($k->currency == 2){
//                    $k->currency_name = '美元';
//                }elseif ($k->currency == 3){
//                    $k->currency_name = '欧元';
//                }else{
//                    $k->currency_name = '';
//                }

                $k->currency_name = IndexController::currencyNameCommon($k->currency);

            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }

    public static function find($condition)
    {
        $res = Db::table('wa_orders_quotation')
            ->where($condition)
            ->first();
        if ($res){

            if ($res->including_model){
                $res->including_model = json_decode($res->including_model);
            }

            $where_bill_details = [];
            $where_bill_details [] = ['quotation_order_sn', '=', $res->quotation_order_sn];
            $bill_details = WaOrdersQuotationAmountDetail::all($where_bill_details);

            if ($bill_details) {
                $bill_details = json_decode($bill_details);


                foreach ($bill_details as $ke) {
                    if ($ke->quotation_order_sn) {
//                        $where_payroll = [];
//                        $where_payroll [] = ['quotation_order_sn', '=', $ke->quotation_order_sn];
//                        $where_payroll [] = ['price_name', '=', $ke->price_name];
//                        $payroll_requisition = WaPurchasePayrollRequisition::allByPur($where_payroll);
//                        $payroll_requisition = json_decode($payroll_requisition);
//
//
//                        $ke->requested_price = WaPurchasePayrollRequisition::sums($where_payroll);
//                        if ($payroll_requisition) {
//                            // 总金额
//                            $ke->total_amount =  round($ke->total_amount,2);
//                            // 已请款
//                            $ke->requested_price = round(WaPurchasePayrollRequisition::sums($where_payroll),2);
//                            // 请款中
//                            $ke->requested_pricing = round(WaPurchasePayrollRequisition::suming($where_payroll),2); ;
//                            //  待请款
//                            $ke->wait_price = $ke->total_amount - $ke->requested_price - $ke->requested_pricing;
//
//                            $ke->payroll_requisition = $payroll_requisition;
//                        } else {
//                            $ke->payroll_requisition = [];
//                        }
                        $ke->payroll_requisition = [];
                    } else {
                        $ke->payroll_requisition = [];
                    }

                }
            }
            $res->bill_details = $bill_details; //  采购账单明细


            // 1 汽运、2 铁路、海运(3 集装箱、4滚装船)
             if ($res->shipment == 1){
                 $res->shipment_name = '汽运';
             }elseif ($res->shipment == 2){
                 $res->shipment_name = '铁路';
             }elseif ($res->shipment == 3){
                 $res->shipment_name = '海运集装箱';
             }elseif ($res->shipment == 4){
                 $res->shipment_name = '海运滚装船';
             }else{
                 $res->shipment_name = '';
             }



            // 报价单状态 0 草稿 1 待客户确认 2 待下单 3 已完成 4 已作废 5 删除
            if ($res->status == 0){
                $res->status_name = '草稿';
            }elseif ($res->status == 1){
                $res->status_name = '待客户确认';
            }elseif ($res->status == 2){
                $res->status_name = '待下单';
            }elseif ($res->status == 3){
                $res->status_name = '已完成';
            }elseif ($res->status == 4){
                $res->status_name = '已作废';
            }elseif ($res->status == 5){
                $res->status_name = '删除';
            }else{
                $res->status_name = '';
            }


            // 订单类型 1 预售待采购   2  库存待销售 3 部分库存待销售
            if ($res->orders_type == 1){
                $res->orders_type_name = '库存待销售';
            }elseif ($res->orders_type == 2){
                $res->orders_type_name = '部分库存待销售';
            }elseif ($res->orders_type == 3){
                $res->orders_type_name = '已完成';
            }else{
                $res->orders_type_name = '';
            }

            // 交货条件 1 FAS  2 FOB  3 CIF  4 CIP
//            if ($res->condition == 1){
//                $res->condition_name = 'FCA';
//            }elseif ($res->condition == 2){
//                $res->condition_name = 'FOB';
//            }elseif ($res->condition == 3){
//                $res->condition_name = 'CIF';
//            }elseif ($res->condition == 4){
//                $res->condition_name = 'CIP';
//            }else{
//                $res->condition_name = '';
//            }
            $res->condition_name = IndexController::conditionName($res->condition);


            // 货币类型   1 人民币 2 美元 3 欧元
//            if ($res->currency == 1){
//                $res->currency_name = '人民币';
//            }elseif ($res->currency == 2){
//                $res->currency_name = '美元';
//            }elseif ($res->currency == 3){
//                $res->currency_name = '欧元';
//            }else{
//                $res->currency_name = '';
//            }

            $res->currency_name = IndexController::currencyNameCommon($res->currency);

        }
        return json_encode($res);
    }

    public static function quotationOrdersCountByAdmin($start_time,$end_time,$adminId)
    {

        $start_time = date('Y-m-d H:i:s', $start_time);
        $end_time = date('Y-m-d H:i:s', $end_time);
        $provider_count =Db::table('wa_orders_quotation')
            ->select('*')
            ->where('admin_id',$adminId->id)
            ->whereBetween('created_at',[$start_time,$end_time])
            ->count(); // 用户数量
        return $provider_count;

    }


}